<?php
class SiteManager_ReferencesController extends SiteManagerController
{

   	public function indexAction() {
   		$t = new Obj_DBTable_References();
   		$references = $t->getAdminReferences();
   		$this->view->references = $references;
    }
    
    public function deleteAction()
    {
    	if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$id = intval($this->getRequest()->getParam('id', 0));
    	$t = new Obj_DBTable_References();
    	$t->delete('ID = '.$id);
    	$this->_helper->json(array(), true, false);
    }

    public function changeApprovedAction()
    {
    	if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$id = intval($this->getRequest()->getParam('id', 0));
    	$t = new Obj_DBTable_References();
    	$ar = $t->getReference($id);
    	if ($ar !== false) {
    		if ($ar['Approved'] == 1) $app = 0;
    		else $app = 1;
    		$t->update(array('Approved' => $app), 'ID = '.$id);
    	}
    	$this->_helper->json(array(), true, false);
    }

   	public function editAction() {
   		$id = intval($this->getRequest()->getParam('id', 0));
   		$t = new Obj_DBTable_References();
   		$ref = $t->getReference($id);
   		if ($ref === false) {
   			$this->_redirect(HTTP_HOST.'/site-manager/references');
   			return;
   		}
   		$this->view->reference = $ref;
    }
    
    public function saveAction()
    {
    	if (!$this->_isAJAX) {
    		throw new Zend_Exception(_('Эта функция Вам недоступна'));
    	}
    	$id = intval($this->getRequest()->getParam('id', 0));
    	$name = trim($this->getRequest()->getParam('name', ''));
    	$email = trim($this->getRequest()->getParam('email', ''));
    	$reference = trim($this->getRequest()->getParam('reference', ''));
    	
    	$t = new Obj_DBTable_References();
    	$ar = array(
    		'Name' => $name,
    		'Email' => $email,
    		'Reference' => $reference,
    	);
    	
    	$t->update($ar, 'ID = '.$id);
    	
    	$this->_helper->json(array(), true, false);
    }
    

}